home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 July / macformat52.iso / mac / Shareware Plus / Educational / LEE 2.1 / Source / macinterface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-08  |  9.8 KB  |  388 lines

  1. /* macinterface.c
  2.  *                         Copyright (1993)
  3.  *
  4.  *          Rik Belew.  University of California, San Diego
  5.  *        Greg Linden.  University of California, San Diego
  6.  *
  7.  *        This software may be redistributed without charge;
  8.  *                 this notice should be preserved.
  9.  */
  10.  
  11.  
  12.  
  13.  
  14. #include "defs.h"
  15.  
  16.  
  17.  
  18.  
  19.  
  20. void HandleMenuChoice (menuChoice)
  21.     long int        menuChoice;
  22. {
  23.     Str255         accName;
  24.     int            dummy, menuItem, accNumber;
  25.     boolean        isSystemEdit;
  26.     
  27.     if (menuChoice!=0) {
  28.         menuItem = LoWord(menuChoice);
  29.     
  30.         switch (HiWord(menuChoice)) {
  31.         
  32.             case APPLE_MENU_ID: 
  33.                 switch (menuItem) {
  34.                     case ABOUT_CHOICE:
  35.                         Alert(400,NIL_POINTER);
  36.                         break;
  37.                     default:
  38.                         GetItem(GetMHandle(APPLE_MENU_ID),menuItem,accName);
  39.                         accNumber = OpenDeskAcc(accName);
  40.                         break;
  41.                 }
  42.                 break;
  43.                 
  44.             case FILE_MENU_ID:  
  45.                 switch (menuItem) {
  46.                     case QUIT_CHOICE: 
  47.                         gDone = TRUE;
  48.                         verbose = 0;
  49.                         break;
  50.                     default:
  51.                         break;
  52.                 }
  53.                 break;
  54.                 
  55.             case EDIT_MENU_ID: 
  56.                 isSystemEdit = SystemEdit(menuItem - 1);
  57.                 break;
  58.                 
  59.             case OPTIONS_MENU_ID:
  60.                 switch (menuItem) {
  61.                 
  62.                     case DISPLAY_CHOICE:
  63.                         if (gInteractive == FALSE) {
  64.                             gInteractive = TRUE;
  65.                             CheckItem(gOptionsMenu, DISPLAY_CHOICE, TRUE);
  66.                             ShowWindow(gTheWindow);
  67.                             SelectWindow(gTheWindow);
  68.                             initDrawWorld();
  69.                             updateGeneration();
  70.                         }
  71.                         else {
  72.                             gInteractive = FALSE;
  73.                             CheckItem(gOptionsMenu, DISPLAY_CHOICE, FALSE);
  74.                             FillRect(&(gTheWindow->portRect), &qd.white /* CW: qd.white */ );
  75.                             HideWindow(gZoomCellWindow);
  76.                             HideWindow(gTheWindow);
  77.                         }
  78.                         break;
  79.                         
  80.                     case LASSO_CHOICE:
  81.                         if (gLassoOn == FALSE) {
  82.                             gLassoOn = TRUE;
  83.                             CheckItem(gOptionsMenu, LASSO_CHOICE, TRUE);
  84.                         }
  85.                         else {
  86.                             gLassoOn = FALSE;
  87.                             CheckItem(gOptionsMenu, LASSO_CHOICE, FALSE);
  88.                         }
  89.                         break;
  90.  
  91.                     case L_VERBOSE_CHOICE:
  92.                         if (verbose > 1) verbose--;
  93.                         else if (verbose == 1)
  94.                         {
  95.                             printf("\n\nWarning: you will not be able to turn verbose back on!\n");
  96.                             verbose = 0;
  97.                         }
  98.                         break;
  99.                         
  100.                     case M_VERBOSE_CHOICE:
  101.                         if ((verbose > 0) && (verbose < 4)) verbose++;
  102.                         break;
  103.                         
  104.                     case PAUSE_CHOICE:
  105.                         if (gPaused == FALSE) {
  106.                             gPaused = TRUE;
  107.                             CheckItem(gOptionsMenu, PAUSE_CHOICE, TRUE);
  108.                         }
  109.                         else {
  110.                             gPaused = FALSE;
  111.                             CheckItem(gOptionsMenu, PAUSE_CHOICE, FALSE);
  112.                         }
  113.                         break;
  114.                         
  115.                     default:
  116.                         break;
  117.                         
  118.                 }
  119.                 break;
  120.         }
  121.         HiliteMenu(0);
  122.     }
  123. }
  124.  
  125.  
  126.  
  127.  
  128. void UpdateZoomWindow() {
  129.     int        counter;
  130.     cell    *theCell = world[gCurrentZoomCell.h][gCurrentZoomCell.v];
  131.     boolean    resetCell = FALSE;
  132.     
  133.     if ((gInteractive) && (FrontWindow() == gZoomCellWindow)) {
  134.         counter = gCurrentZoomItem;
  135.         while (counter>0) {
  136.             if (theCell->next == NIL_POINTER) {
  137.                 counter = 0;
  138.                 resetCell = TRUE;
  139.             }
  140.             else {
  141.                 theCell = theCell->next;
  142.                 counter--;
  143.             }
  144.         }
  145.     
  146.         if (resetCell)
  147.             gCurrentZoomItem = 0;
  148.             
  149.         SelectCell (gCurrentZoomCell.h, gCurrentZoomCell.v, gCurrentZoomItem);
  150.     }
  151. }
  152.  
  153.  
  154. void    HandleInContent (globalPoint, theWindow)
  155. Point        globalPoint;
  156. WindowPtr    theWindow;
  157. {
  158.     Point            Where;
  159.     Rect            r;
  160.     GrafPtr            oldPort;
  161.     cell            *theCell;
  162.     int                counter, top, bottom, left, right;
  163.     PenState        thePen;
  164.  
  165.  
  166.     GetPort(&oldPort);
  167.     SetPort(theWindow);
  168.     Where = globalPoint;
  169.     GlobalToLocal(&Where);
  170.     SetPort(oldPort);
  171.  
  172.     
  173.     if (gTheWindow == theWindow) {
  174.         if ((Where.v>=START_Y) && (Where.h>=START_X) && (Where.v<=SQUARE_SIZE*y_dim+START_Y) && (Where.h<=SQUARE_SIZE*x_dim+START_X)) 
  175.         {
  176.             if ((gCurrentZoomCell.h!=(Where.h-START_X) / SQUARE_SIZE) || (gCurrentZoomCell.v!=(Where.v-START_Y) / SQUARE_SIZE)) {
  177.                 if ((gCurrentZoomCell.h!=-1) && (gCurrentZoomCell.v!=-1)) {
  178.                     GetPenState(&thePen);
  179.                     PenPat(&qd.white /* CW: qd.white */ );
  180.                     SetRect(&r, START_X+gCurrentZoomCell.h*SQUARE_SIZE, START_Y+gCurrentZoomCell.v*SQUARE_SIZE, START_X+(gCurrentZoomCell.h+1)*SQUARE_SIZE - GAP_SIZE, START_Y+(gCurrentZoomCell.v+1)*SQUARE_SIZE-GAP_SIZE);
  181.                     SetRect(&r, r.left-2, r.top-2,r.right+2, r.bottom+2);
  182.                     FrameRect(&r);
  183.                     SetPenState(&thePen);
  184.                 }
  185.                 
  186.                 gCurrentZoomCell.h = (Where.h-START_X) / SQUARE_SIZE;
  187.                 gCurrentZoomCell.v = (Where.v-START_Y) / SQUARE_SIZE;
  188.                 gCurrentZoomItem = 0;
  189.             }
  190.             drawCell(gCurrentZoomCell.h, gCurrentZoomCell.v);
  191.             
  192.             GetPenState(&thePen);
  193.             SetRect(&r, START_X+gCurrentZoomCell.h*SQUARE_SIZE, START_Y+gCurrentZoomCell.v*SQUARE_SIZE, START_X+(gCurrentZoomCell.h+1)*SQUARE_SIZE - GAP_SIZE, START_Y+(gCurrentZoomCell.v+1)*SQUARE_SIZE-GAP_SIZE);
  194.             SetRect(&r, r.left-2, r.top-2,r.right+2, r.bottom+2);
  195.             FrameRect(&r);
  196.             SetPenState(&thePen);
  197.             
  198.             if (!(FrontWindow() == gZoomCellWindow))
  199.                 SelectCell(gCurrentZoomCell.h, gCurrentZoomCell.v, gCurrentZoomItem);
  200.         }
  201.         else
  202.             SysBeep(5);
  203.     }
  204.     else if (gZoomCellWindow == theWindow) {
  205.         top = gZoomCellWindow->portRect.bottom - gZoomCellWindow->portRect.top - ARROW_OFFSET_BOTTOM;
  206.         left = (gZoomCellWindow->portRect.right - gZoomCellWindow->portRect.left) / 2 - ARROW_SIZE - ARROW_OFFSET_MIDDLE;
  207.         bottom = top + ARROW_SIZE;
  208.         right = left + ARROW_SIZE;
  209.         SetRect(&r, left , top, right, bottom);
  210.         if ((PtInRect(Where, &r)) && (gCurrentZoomItem>0)) {
  211.             gCurrentZoomItem--;
  212.             SelectCell(gCurrentZoomCell.h, gCurrentZoomCell.v, gCurrentZoomItem);
  213.         }
  214.         else {
  215.             top = gZoomCellWindow->portRect.bottom - gZoomCellWindow->portRect.top - ARROW_OFFSET_BOTTOM;
  216.             left = (gZoomCellWindow->portRect.right - gZoomCellWindow->portRect.left) / 2 + ARROW_OFFSET_MIDDLE;
  217.             bottom = top + ARROW_SIZE;
  218.             right = left + ARROW_SIZE;
  219.             SetRect(&r, left , top, right, bottom);
  220.             counter = gCurrentZoomItem;
  221.             theCell = world[gCurrentZoomCell.h][gCurrentZoomCell.v];
  222.             while (counter>0) {
  223.                 theCell = theCell->next;
  224.                 counter--;
  225.             }
  226.             if ((PtInRect(Where, &r)) && (theCell->next!=NIL_POINTER)) {
  227.                 gCurrentZoomItem++;
  228.                 SelectCell(gCurrentZoomCell.h, gCurrentZoomCell.v, gCurrentZoomItem);
  229.             }
  230.             else
  231.                 SysBeep(5);
  232.         }
  233.     }
  234. }
  235.  
  236.  
  237.  
  238.  
  239. void    HandleMouseDown (theEvent)
  240. EventRecord        theEvent;
  241. {
  242.     WindowPtr        whichWindow;
  243.     Rect            r;
  244.     PenState        thePen;
  245.  
  246.     switch (FindWindow(theEvent.where, &whichWindow)) {
  247.         case inMenuBar: 
  248.             HandleMenuChoice(MenuSelect(theEvent.where));
  249.             break;
  250.         case inSysWindow: 
  251.             SystemClick(&theEvent, whichWindow);
  252.             break;
  253.         case inContent: 
  254.             if (gInteractive)
  255.                 HandleInContent(theEvent.where, whichWindow);
  256.             else
  257.                 SysBeep(5);
  258.             break;
  259.         case inDrag:
  260.             if (whichWindow==gZoomCellWindow)
  261.                 DragWindow(gZoomCellWindow, theEvent.where, &qd.screenBits.bounds);
  262.             else if (whichWindow==gTheWindow) {
  263.                 if (FrontWindow()==gZoomCellWindow) {
  264.                     HideWindow(gZoomCellWindow);
  265.                     
  266.                     GetPenState(&thePen);
  267.                     PenPat(&qd.white /* CW: qd.white */ );
  268.                     SetRect(&r, START_X+gCurrentZoomCell.h*SQUARE_SIZE, START_Y+gCurrentZoomCell.v*SQUARE_SIZE, START_X+(gCurrentZoomCell.h+1)*SQUARE_SIZE - GAP_SIZE, START_Y+(gCurrentZoomCell.v+1)*SQUARE_SIZE-GAP_SIZE);
  269.                     SetRect(&r, r.left-2, r.top-2,r.right+2, r.bottom+2);
  270.                     FrameRect(&r);
  271.                     SetPenState(&thePen);
  272.                 }
  273.                 DragWindow(gTheWindow, theEvent.where, &qd.screenBits.bounds);
  274.             }
  275.             break;
  276.         case inGoAway:
  277.             if (whichWindow==gZoomCellWindow)
  278.                 if (TrackGoAway(gZoomCellWindow, theEvent.where)) {
  279.                     HideWindow(gZoomCellWindow);
  280.                     
  281.                     GetPenState(&thePen);
  282.                     PenPat(&qd.white /* CW: qd.white */ );
  283.                     SetRect(&r, START_X+gCurrentZoomCell.h*SQUARE_SIZE, START_Y+gCurrentZoomCell.v*SQUARE_SIZE, START_X+(gCurrentZoomCell.h+1)*SQUARE_SIZE - GAP_SIZE, START_Y+(gCurrentZoomCell.v+1)*SQUARE_SIZE-GAP_SIZE);
  284.                     SetRect(&r, r.left-2, r.top-2,r.right+2, r.bottom+2);
  285.                     FrameRect(&r);
  286.                     SetPenState(&thePen);
  287.                 }
  288.             break;
  289.         case inDesk:
  290.             SysBeep(5);
  291.             break;
  292.     }
  293. }
  294.  
  295.  
  296.  
  297.  
  298. void HandleUpdate (theWindow)
  299. WindowPtr theWindow;
  300. {
  301.     GrafPtr    oldPort;
  302.     PenState thePen;
  303.     Rect     r;
  304.     
  305.     GetPort(&oldPort);
  306.     
  307.     SetPort(theWindow);
  308.     BeginUpdate(theWindow);
  309.     if (theWindow == gTheWindow) {
  310.         if (gInteractive) {
  311.             initDrawWorld();
  312.             if ((gCurrentZoomCell.h!=-1) && (gCurrentZoomCell.v!=-1) && (FrontWindow() == gZoomCellWindow)) {
  313.                 GetPenState(&thePen);
  314.                 PenPat(&qd.black /* CW: qd.black */ );
  315.                 SetRect(&r, START_X+gCurrentZoomCell.h*SQUARE_SIZE, START_Y+gCurrentZoomCell.v*SQUARE_SIZE, START_X+(gCurrentZoomCell.h+1)*SQUARE_SIZE - GAP_SIZE, START_Y+(gCurrentZoomCell.v+1)*SQUARE_SIZE-GAP_SIZE);
  316.                 SetRect(&r, r.left-2, r.top-2,r.right+2, r.bottom+2);
  317.                 FrameRect(&r);
  318.                 SetPenState(&thePen);
  319.             }
  320.             updateGeneration();
  321.         }
  322.     }
  323.     else if (theWindow == gZoomCellWindow) {
  324.         if (gInteractive)
  325.             UpdateZoomWindow();
  326.     }
  327.     EndUpdate(theWindow);
  328.     
  329.     SetPort(oldPort);
  330. }
  331.  
  332.  
  333.  
  334. void    HandleEvent () 
  335. {
  336.     EventRecord        theEvent;
  337.     char            theChar;
  338.     Point            aPoint = {100,100};
  339.     PenState        thePen;
  340.     Rect            r;
  341.     
  342.     if (WaitNextEvent(everyEvent, &theEvent, 1L, NIL_POINTER))
  343.     {
  344. #ifdef __MWERKS__
  345.         SIOUXHandleOneEvent(&theEvent);
  346. #endif
  347.         switch (theEvent.what)
  348.         {
  349.             case nullEvent:
  350.                 break;
  351.             case updateEvt: 
  352.                 HandleUpdate((WindowPtr)theEvent.message);
  353.                 break;
  354.             case mouseDown: 
  355.                 HandleMouseDown(theEvent);
  356.                 break;
  357.             case keyDown:
  358.             case autoKey: 
  359.                 theChar = theEvent.message & charCodeMask;
  360.                 if ((theEvent.modifiers & cmdKey) != 0) 
  361.                     HandleMenuChoice(MenuKey(theChar));
  362.                 break;
  363.             case activateEvt: 
  364.                 if (((theEvent.modifiers & activeFlag)==0) && ((WindowPtr)theEvent.message == gZoomCellWindow)){
  365.                     HideWindow(gZoomCellWindow);
  366.                     
  367.                     GetPenState(&thePen);
  368.                     PenPat(&qd.white /* CW: qd.white */ );
  369.                     SetRect(&r, START_X+gCurrentZoomCell.h*SQUARE_SIZE, START_Y+gCurrentZoomCell.v*SQUARE_SIZE, START_X+(gCurrentZoomCell.h+1)*SQUARE_SIZE - GAP_SIZE, START_Y+(gCurrentZoomCell.v+1)*SQUARE_SIZE-GAP_SIZE);
  370.                     SetRect(&r, r.left-2, r.top-2,r.right+2, r.bottom+2);
  371.                     FrameRect(&r);
  372.                     SetPenState(&thePen);
  373.                 }
  374.                 break;
  375.             case diskEvt:
  376.                 if (HiWord(theEvent.message)!=noErr)
  377.                     (void) DIBadMount(aPoint, theEvent.message);
  378.                 break;
  379.             default:
  380.                 break;
  381.         }
  382.     }
  383. }
  384.  
  385.  
  386.  
  387.  
  388.